import {messages} from "share/common";
import React from 'react'
import { connect } from 'react-redux'
import moment from 'moment'
import menuRoute from 'routes/menuRoute'
import { Row, Icon } from 'antd'
import Condition from 'components/condition'
const If = Condition.If;

class RelativeExpenseReportDetail extends React.Component{
  constructor(props) {
    super(props);
    this.state = {
      requestDetail: menuRoute.getRouteItem('request-detail'), //申请单-申请单详情（公司模式）
      requestDetailCost: menuRoute.getRouteItem('cost-application-detail'), //财务管理-费用申请单查看-费用申请单详情（公司／集团模式）
      loanRequestDetail: menuRoute.getRouteItem('loan-detail'),
      loanRequestDetailPayment: menuRoute.getRouteItem('loan-refund-detail'),
      contractDetail: menuRoute.getRouteItem('contract-detail')
    }
  }

  handleGoApplication = (item, flag) => {
    let jumpObj = {
      application: () => {
        if (this.props.tenantMode) { //集团模式下
      window.open((window.ISIE9 ? "#" : "") + this.state.requestDetailCost.url.replace(':formOID', item.applicationFormOID).replace(':applicationOID', item.applicationOID) + '?from=expense');
    } else { //公司模式下
      window.open((window.ISIE9 ? "#" : "") + this.state.requestDetail.url.replace(':formOID', item.applicationFormOID).replace(':applicationOID', item.applicationOID) + '?from=expense');
    }
      },
      contract: () => {
        window.open((window.ISIE9 ? '#' : '') + this.state.contractDetail.url.replace(':id', item));
      }
    };
    jumpObj[flag]();

  };
  handleGoLoanApplication = () => {
    const { info,pageFrom } = this.props;
    if (pageFrom === 'myView') {
      window.open((window.ISIE9 ? "#" : "") + this.state.loanRequestDetail.url.replace(':formOID', info.loanApplicationFormOID).replace(':loanBillOID', info.loanApplicationOID).replace(':pageFrom', 'my'));
    }
    if (pageFrom === 'approveView') {
      window.open((window.ISIE9 ? "#" : "") + this.state.loanRequestDetail.url.replace(':formOID', info.loanApplicationFormOID).replace(':loanBillOID', info.loanApplicationOID));
    }
    if (pageFrom === 'auditView') {
      window.open((window.ISIE9 ? "#" : "") + this.state.loanRequestDetailPayment.url.replace(':formOID', info.loanApplicationFormOID).replace(':loanBillOID', info.loanApplicationOID));
    }
  };


  render() {
    const { info } = this.props;
    info.travelStartDate && (info.travelStartDate = moment(info.travelStartDate).format('YYYY-MM-DD'));
    info.travelEndDate && (info.travelEndDate = moment(info.travelEndDate).format('YYYY-MM-DD'));
    let expenseReportApplicationDTOS = info.expenseReportApplicationDTOS || [];
    let application = (
      <section>
        <If value={expenseReportApplicationDTOS.length}>
          {expenseReportApplicationDTOS.map(item => {
            return (
              <Row className="row-container" key={item.applicationOID}>
                <Icon type="link" className="link-icon"/>
                <a onClick={() => this.handleGoApplication(item,'application')}>
                  {messages('expense-parent-report.key63'/*申请单*/)}：{item.applicationBusinessCode}
                </a>
                <If value={info.travelStartDate && info.travelEndDate}>
                  <span style={{ marginLeft: '10px', color: '#666' }}>
                    {messages('expense-parent-report.key64'/*往返日期*/)}：
                    {moment(item.travelStartDate)
                      .format('YYYY-MM-DD')}~{moment(item.travelEndDate)
                    .format('YYYY-MM-DD')}
                  </span>
                </If>
              </Row>
            );
          })}
        </If>
        <If value={info.contractHeaderId} >
          <Row onClick={() => this.handleGoApplication(info.contractHeaderId,'contract')}>
            <Icon type="link" className="link-icon"/>
            <a>
              {messages('expense-parent-report.key194')/*合同*/}：{info.contractNumber}
            </a>
          </Row>
        </If>
      </section>
    );
    let loanApplication=info.loanApplicationBusinessCode ?  (
      <span style={{marginRight:20}}>
        <Icon type="link" className="link-icon"/>
        <a onClick={this.handleGoLoanApplication}>{messages('expense-parent-report.key54')/*借款单*/}：{info.loanApplicationBusinessCode}</a>
      </span>
    ) : null;
    return (
      <Row className="row-container">
        {application}
        {/*借款单暂时隐藏，之后可能会有逻辑更改*/}
        {/*{loanApplication}*/}
      </Row>
    )
  }
}

RelativeExpenseReportDetail.propTypes = {
  info: React.PropTypes.object,
  pageFrom: React.PropTypes.string
};

RelativeExpenseReportDetail.defaultProps={
  info: {},
  pageFrom: "approveView"
};

RelativeExpenseReportDetail.contextTypes = {
  router: React.PropTypes.object
};

function mapStateToProps(state) {
  return {
    tenantMode: state.main.tenantMode
  }
}

export default connect(mapStateToProps)(RelativeExpenseReportDetail)
